<?php             
class Recipe extends MY_Controller{				
	function __construct()
	{
		parent::__construct();
	}
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
        
    function index(){
        $this->load->model('Language');
        $data = array();
        foreach($this->Language->get_all() as $row){
                $data['langList'][$row['language_code']] = $row['language_name'];
        }
    	$this->_setLayout('admin/recipe/recipe_list','admin_layout',$data);
    }
            
		
	function delete(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
			$this->load->model('Recipe_model');
			$this->Recipe_model->delete($data);
			$jsonOutPut =  json_encode(array('success'=>true));
		}
		catch (Exception $e){
			$jsonOutPut =  json_encode(array('success'=>false));
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function grid(){
		$this->load->model('Recipe_model');
		$callback = $_REQUEST['callback'];
		$data =  array();
		$listRecipe = $this->Recipe_model->get_list($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'order',(isset($_GET['dir']))?$_GET['dir']:'ASC');
                foreach($listRecipe as $row){
			$data[] = array_merge($row,array('edit'=>'<a href="'.base_url().'admin/recipe/edit/'.$row['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>',
                            'thumb'=>'<img style="max-width:150px;max-height:150px" src="'.base_url().'public/upload/fix/'.$row['images'].'" />'));
		}
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->Recipe_model->get_total(),'data'=>$data));
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}
	}
			
	function edit($id=0)
	{
		$this->load->model('Recipe_model');
		$this->load->model('Language');
		$this->load->library('helpfunc');
                $pItem = $this->Recipe_model->get_by_id($id);
                
		$data = array('datacontent' => (empty($id))?array():$pItem,
			      'message' => null);
                $total_item = $this->Recipe_model->get_total();
                for($i = 1; $i <= $total_item;$i++ ){
                    $data['total_item'][$i] = $i;
                }
                $oldOrder = array_key_exists('order', $pItem) ? $pItem['order'] : 0;
		
		if($this->input->server('REQUEST_METHOD') == 'POST'){
			$input_data = $_POST;
                         if($_FILES['images']['name'] !='')
			{
				$dir_upload = str_replace('//','/',getcwd() . '/public/upload/');
				$dir_fix = str_replace('//','/',$dir_upload.'/fix/');
				if(!empty($data['datacontent']['images'])){
                                    if (is_file($dir_upload.$data['datacontent']['images']))
					unlink($dir_upload.$data['datacontent']['images']);
                                    if (is_file($dir_fix.$data['datacontent']['images']))
					unlink($dir_fix.$data['datacontent']['images']);
				}
				$prefix = 'products';
				$file_input_tmp = $_FILES['images']['tmp_name'];
				$file_input_name = $_FILES['images']['name'];
				$image = $this->helpfunc->copy_and_change_filename($file_input_tmp, $file_input_name,$dir_upload, $prefix);
				$this->helpfunc->copy_and_resize($image,$dir_upload,$dir_fix,177,133);
				$input_data['images'] = $image;
			}

                        //-----------Update Order--------------------
                        if (!empty($id)){
                            $this->Recipe_model->updateOrder($oldOrder,$input_data['order']);
                        }else{
                            $input_data['order'] = $total_item + 1;
                        }
                        //-------------------------------------------

			if(!empty($id) && $this->Recipe_model->update($input_data,$id)){
                             header("Location: ".base_url()."admin/recipe/");
				$data['message'] = 'Update successfully';
				$data['datacontent'] = array_merge($data['datacontent'],$input_data);
			}elseif($id = $this->Recipe_model->insert($input_data,$id)){
                            header("Location: ".base_url()."admin/recipe/");
				$data['message'] = 'Update successfully';
				$data['datacontent'] = $this->Recipe_model->get_by_id($id);
			}
			else
				$data['message'] = 'Update fail';

		}
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name'];
		}
		$this->_setLayout('admin/recipe/recipe_edit', 'admin_layout', $data);
	}
            
}
		 ?>